package se.chalmers.tda366.crumble.gui.components;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;

import se.chalmers.tda366.crumble.core.IArticle;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ArticleViewPanel extends JPanel implements MouseListener{
	private JTextPane contentPane;
	private JLabel authorLabel;
	private JScrollPane contentScrollPane;
	private JLabel dateLabel;
	private JLabel sourceLabel;
	private JLabel headlineLabel;
	private URI url;

	public ArticleViewPanel (IArticle article) {
		initGUI(article);
		try {
			url=new URI(article.getSource());
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void initGUI(IArticle article) {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			this.setBackground(new java.awt.Color(255,255,255));
			{
				headlineLabel = new JLabel();
				headlineLabel.setText("Rubrik lol!"); //dummy text for formatting purpuses
				headlineLabel.setText(article.getHeadline());
				headlineLabel.setFont(se.chalmers.tda366.crumble.resources.Constants.HEADLINEFONT);
			}
			{
				sourceLabel = new JLabel();
				//sourceLabel.setText("http://www.konstigadress.com/article.php"); //dummy text for formatting purposes
				sourceLabel.setText("<html><a href='"+article.getSource()+"'>"+article.getSource()+"</a></html>");
				sourceLabel.setFont(se.chalmers.tda366.crumble.resources.Constants.LINK);
				sourceLabel.addMouseListener(this);
			}
			{
				authorLabel = new JLabel();
				authorLabel.setText("authour");
			}
			{
				dateLabel = new JLabel();
				dateLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
				dateLabel.setHorizontalAlignment(SwingConstants.RIGHT);
				//dateLabel.setText("Tue 4 may, 2003-03-02 GMT"); 	//dummy text for formatting purpuses
				dateLabel.setText(article.getDateString().toString());
			}
			{
				contentScrollPane = new JScrollPane();
				contentScrollPane.setBackground(new java.awt.Color(255,255,255));
				contentScrollPane.setEnabled(false);
				contentScrollPane.setOpaque(false);
				{
					contentPane = new JTextPane();
					contentScrollPane.setViewportView(contentPane);
					contentPane.setEditable(false);
					contentPane.setOpaque(false);
					contentPane.setBackground(new java.awt.Color(255,255,255));
					contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
					/*contenPane.setText("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor " +
							"incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " +
							"ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in " +
							"voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
							"proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");//dummy text for formatting purpuses
							//contentLabel.setLineWrap(true);
							//contentLabel.setWrapStyleWord(true);*/
							contentPane.setContentType("text/html");
							contentPane.setText(article.getContent());
							contentPane.setFont(se.chalmers.tda366.crumble.resources.Constants.BODYTEXT);
							contentPane.setPreferredSize(new java.awt.Dimension(453, 168));
				}
			}
				thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(headlineLabel, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(dateLabel, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(contentScrollPane, 0, 304, Short.MAX_VALUE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
					.addComponent(sourceLabel, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
					.addContainerGap());
				thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(thisLayout.createParallelGroup()
					    .addComponent(headlineLabel, GroupLayout.Alignment.LEADING, 0, 532, Short.MAX_VALUE)
					    .addComponent(contentScrollPane, GroupLayout.Alignment.LEADING, 0, 532, Short.MAX_VALUE)
					    .addComponent(sourceLabel, GroupLayout.Alignment.LEADING, 0, 532, Short.MAX_VALUE)
					    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					        .addGap(145)
					        .addComponent(dateLabel, 0, 387, Short.MAX_VALUE)))
					.addContainerGap());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getComponent().equals(sourceLabel)){
	
			try {
				java.awt.Desktop.getDesktop().browse(url);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		sourceLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		//TODO
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
